package com.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Scanner;

import javax.servlet.ServletResponse;

public class MessageSender extends Thread  {
	private ServletResponse connection;

	public MessageSender(ServletResponse connection) {
		this.connection = connection;
	}
	
	public void run() {
		try {
			while (true) {
				if (connection == null) {
					synchronized (this) {
						wait();
					}
				}

				OutputStream out = connection.getOutputStream();
				Scanner s = new Scanner(System.in);
				System.out.println("ceshi");
				String ss = s.next();
				out.write(ss.getBytes());
				out.flush();
				connection.flushBuffer();
				
				System.out.print(getString());
				
				Thread.sleep(1000);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("deprecation")
	private String getString() {
		return Thread.currentThread()+" CurrentTime "+new Date().toLocaleString() + "\n";
	}
}
